﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using System.Collections;
using StockImport.dataimport.otc.dailyindex;
using StockImport.http;
using HAP = HtmlAgilityPack;
using StockCommon;
using StockCommon.sidehelper;

namespace StockImport.dataimport.otc
{
    public class Otc_CategoryUpdater : HttpContentFetcher
    {



        protected static List<String> 上櫃市產業分類編號 = null;
        Hashtable catCodeToName = new Hashtable();
        protected List<String> getIndustryTypeIds()
        {
            if (上櫃市產業分類編號 != null)
                return 上櫃市產業分類編號;
            List<String> r = new List<string>();
            String[] rs ={"02","03","04","05","06","21","08","10","11",
                                   "14","15","16","17","18","20","22","23","24",
                                   "25","26","27","28","29","31","30","80"};

            catCodeToName.Add("02", "食品工業");
            catCodeToName.Add("03", "塑膠工業");
            catCodeToName.Add("04", "紡織纖維");
            catCodeToName.Add("05", "電機機械");
            catCodeToName.Add("06", "電器電纜");
            catCodeToName.Add("21", "化學工業");
            catCodeToName.Add("08", "玻璃陶瓷");
            catCodeToName.Add("10", "鋼鐵工業");
            catCodeToName.Add("11", "橡膠工業");
            catCodeToName.Add("14", "建材營造");
            catCodeToName.Add("15", "航運業");
            catCodeToName.Add("16", "觀光事業");
            catCodeToName.Add("17", "金融業");
            catCodeToName.Add("18", "貿易百貨");
            catCodeToName.Add("20", "其他");
            catCodeToName.Add("22", "生技醫療類");
            catCodeToName.Add("23", "油電燃氣類");
            catCodeToName.Add("24", "半導體類");
            catCodeToName.Add("25", "電腦及週邊類");
            catCodeToName.Add("26", "光電業類");
            catCodeToName.Add("27", "通信網路類");
            catCodeToName.Add("28", "電子零組件類");
            catCodeToName.Add("29", "電子通路類");
            catCodeToName.Add("31", "其他電子類");
            catCodeToName.Add("30", "資訊服務類");
            catCodeToName.Add("80", "管理股票");
            

            foreach (String s in rs)
                r.Add(s);
            上櫃市產業分類編號 = r;
            return 上櫃市產業分類編號;
        }
        protected String getURL(String id, DateTime d)
        {
            String template = "http://www.otc.org.tw/php/result.php?url=http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/squote_{0}_${yy}${MM}${dd}.html";
            String r1 = replaceYMD(template, d);
            String url = String.Format(r1, id);
            return url;
        }

        public void processIndustyType(DateTime targetDate)
        {
            targetDate = DateUtils.getValidTradeDate(targetDate);
            List<String> catCodes = getIndustryTypeIds();
            int seq = 0;
            foreach (String catcode in catCodes)
            {
                Hashtable codeNames = new Hashtable();
                ExtraceSecurities(targetDate, catcode, codeNames);
                seq++;
                if (codeNames.Count == 0)
                    continue;
                uppdateIndustyType((String)catCodeToName[catcode], catcode, codeNames, seq);
                codeNames.Clear();
            }
        }

        protected void uppdateIndustyType(String catName, String catcode, Hashtable codeNames, int seq)
        {
            Guid catId = StockManager.UpdateOtcBasicCategory(catName, catcode, seq);

            //1.更新category = base.
            foreach (String k in codeNames.Keys)
            {
                String name = (String)codeNames[k];
                if (StockManager.updateOtcStockCategory(k, catName, catId))
                    continue;
                UnCategoriedStockManager.AddUnCategoriedStock(k, name, "otc", catName, catId);
            }
        }

        private void ExtraceSecurities(DateTime targetDate, String catcode, Hashtable codeNames)
        {
            String url = getURL(catcode, targetDate);
            UpdateResult httpResult = UrlGet.process(url);


            if (httpResult.hasContent())
            {
                HAP.HtmlDocument doc = new HAP.HtmlDocument();
                doc.LoadHtml(httpResult.getContent());

                HAP.HtmlNode contentTable = doc.DocumentNode.SelectSingleNode("//table[@id='contentTable']");
                if (contentTable == null)
                    return;

                HAP.HtmlNodeCollection trs = contentTable.SelectNodes(contentTable.XPath + "/tr");
                if (trs != null)
                {
                    foreach (HAP.HtmlNode tr in trs)
                    {
                        if (tr.ChildNodes.Count < 10)
                            continue;
                        String code = tr.ChildNodes[0].InnerText.Trim();
                        String securityName = tr.ChildNodes[1].InnerText.Trim();
                        if (code.Contains("代號"))
                            continue;
                        codeNames.Add(code, securityName);
                    }
                }

            }

        }





        public override string getTaskName()
        {
            throw new NotImplementedException();
        }
        public override string getVersion()
        {
            throw new NotImplementedException();
        }
        public override bool suitableForDate(DateTime d)
        {
            return true;
        }
    }
}
